//
//  CBPickView.swift
//  baihuiju
//
//  Created by 赵家彬 on 2018/6/13.
//  Copyright © 2018年 赵家彬. All rights reserved.
//

import UIKit

class CBPickView: UIView, UIPickerViewDelegate, UIPickerViewDataSource {

    var arrData:[String] = [String]()
    var selectValue:String = ""
    var blkConfirm:((String)->Void)!
    var title:String!
    
    init(arrData:[String], strTitle:String = "", blkConfirm:@escaping (String)->Void) {
        super.init(frame: .zero)
        self.blkConfirm = blkConfirm
        self.title = strTitle
        self.arrData = arrData
        self.cb_size = CGSize(width: S_Width, height: S_Height)
        self.backgroundColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.6)
        
        setupMenu()
        setupPickView()
    }
    
    //生成菜单
    var vwMenu:UIView!
    func setupMenu() {
        vwMenu = UIView(size: CGSize(width: S_Width, height: 40.hr))
        vwMenu.cb_top = S_Height - 240.hr
        vwMenu.backgroundColor = UIColor(hex:0xf6fafc)
        self.addSubview(vwMenu)
        
        let lblTitle = UILabel(text: title, color: UIColor(hex:0x333333), fontSize: 15.wr, isBold: false)
        lblTitle.cb_centerX = S_Width * 0.5
        lblTitle.cb_centerY = vwMenu.cb_height * 0.5
        vwMenu.addSubview(lblTitle)
        
        let btnConfirm = UIButton(text: "确认", color: UIColor(hex:0x6a6d72), fontSize: 14.wr, target: self, selector: #selector(confirm))
        btnConfirm.cb_right = S_Width - 24.wr
        btnConfirm.cb_centerY = vwMenu.cb_height * 0.5
        vwMenu.addSubview(btnConfirm)
    }
    
    //生成选择器
    var vwPick:UIPickerView!
    func setupPickView() {
        vwPick = UIPickerView(frame: CGRect(x: 0, y: vwMenu.cb_bottom, width: S_Width, height: 200.hr))
        vwPick.delegate = self
        vwPick.dataSource = self
        vwPick.backgroundColor = .white
        self.addSubview(vwPick)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //展示
    func show(strSelectValue:String) {
        if let delegate = UIApplication.shared.delegate as? AppDelegate {
            delegate.window?.addSubview(self)
        }
        self.selectValue = arrData.first ?? ""
        self.vwPick.selectRow(0, inComponent: 0, animated: false)

        if let index = arrData.index(of: strSelectValue) {
            self.selectValue = strSelectValue
            self.vwPick.selectRow(index, inComponent: 0, animated: false)
        }
    }
    
    func hide() {
        self.removeFromSuperview()
    }
    
    //确认
    @objc func confirm() {
        if(selectValue.isEmpty) {
            selectValue = arrData.first ?? ""
        }
        self.blkConfirm(selectValue)
        self.hide()
    }
    
    //列数
    func numberOfComponents(in pickerView: UIPickerView) -> Int {
        return 1
    }
    
    //每列个数
    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        return arrData.count
    }
    
    //每列宽度
    func pickerView(_ pickerView: UIPickerView, widthForComponent component: Int) -> CGFloat {
        return S_Width
    }
    
    //返回当前行的内容,此处是将数组中数值添加到滚动的那个显示栏上
    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
        return arrData[row]
    }
    
    //选中事件处理
    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
        self.selectValue = arrData[row]
    }

}
